Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
'--- Created Date On : 29-Sep-2010 (Gyana)

Public Class frmTemplate
    Dim mlngRecordID As Long ' This is the VisitId
    Dim blnUnload As Boolean
    Dim mblnChanged As Boolean
    Dim mblnActivate As Boolean
    Dim mblnCancelDisplay As Boolean

    Dim mintTemplateId As Integer

    Public Sub New(ByVal TempId As Integer)
        MyBase.New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        mintTemplateId = TempId  ' Pass 0 for creating new Template and 1 for selecting existing template

    End Sub

    Private Sub frmTemplate_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Try
            If mblnActivate = True Then
                mblnActivate = False
                Call PopulateLookupDX(lkTemplate, "Template", "TemplateId", "tblTemplateDoctorNotes", "TemplateId,TemplateName [Template]", , "TemplateName")
              
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub frmTemplate_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try

            Dim intUnload As DialogResult

            If mblnChanged = True Then
                intUnload = MessageBox.Show("You have not saved your changes to the record. Do you want to save your changes now?", gstrMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                If intUnload = Windows.Forms.DialogResult.Cancel Then
                    e.Cancel = True : Exit Sub
                ElseIf intUnload = Windows.Forms.DialogResult.Yes Then
                    blnUnload = True
                    'If blnSaveRecords() = False Then
                    '    e.Cancel = True
                    '    blnUnload = False
                    '    Exit Sub
                    'End If
                    mblnChanged = False
                End If
            End If
            blnUnload = False

        Catch ex As Exception
            blnUnload = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub frmTemplate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Call SetImageControl(Me)
            Me.Icon = System.Drawing.Icon.FromHandle(CType(mdiMain.imgFormIcons.Images(2), Bitmap).GetHicon())
            If mintTemplateId = 0 Then
                grbSelectTemplate.Visible = False
                grbEnterTemplate.Visible = True
                txtTemplate.Focus()
            ElseIf mintTemplateId = 1 Then
                grbSelectTemplate.Visible = True
                grbEnterTemplate.Visible = False
                lkTemplate.Focus()
            End If
            mblnActivate = True
            mblnChanged = False

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Template = ""
        TemplateId = 0
        Me.Close()
    End Sub
    Public Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Try
           
            If mintTemplateId = 0 Then
                If Trim(txtTemplate.Text) = "" Then
                    MessageBox.Show("Please Enter Template Name", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    txtTemplate.Focus()
                    Exit Sub
                End If
                Dim TempID As Integer = Val(strRecordID("tblTemplateDoctorNotes", "TemplateId", "TemplateName=" & SmartReplace(txtTemplate.Text)))
                If TempID > 0 Then
                    MessageBox.Show("This Template Name already exists", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    txtTemplate.Focus()
                    Exit Sub
                End If
                Template = txtTemplate.Text
                TemplateId = 0
            ElseIf mintTemplateId = 1 Then
                If IsDBNullOrNothing(lkTemplate.EditValue) Then
                    MessageBox.Show("Please select Template Name", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    lkTemplate.Focus()
                    Exit Sub
                End If
                TemplateId = lkTemplate.EditValue
                Template = lkTemplate.GetColumnValue("Template")
            End If
            Me.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub lkControls_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        mblnChanged = True
    End Sub

    Private Sub dtTime_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        mblnChanged = True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If mintTemplateId = 0 Then
            txtTemplate.Text = Nothing
            txtTemplate.Focus()
        ElseIf mintTemplateId = 1 Then
            lkTemplate.EditValue = Nothing
            lkTemplate.Focus()
        End If
    End Sub
End Class
